#pragma once

#include "Token.h"
#include "AST.h"
#include <vector>
#include <memory>

namespace MiniDB
{

    class Parser
    {
    public:
        explicit Parser(const std::vector<Token> &tokens);
        std::vector<std::shared_ptr<Statement>> parse();

    private:
        std::vector<Token> tokens_;
        size_t position_;
        Token current_token_;

        void advance();
        Token peek() const;
        Token expect(TokenType type);

        // 语句解析
        std::shared_ptr<Statement> parseStatement();
        std::shared_ptr<SelectStatement> parseSelectStatement();
        std::shared_ptr<InsertStatement> parseInsertStatement();
        std::shared_ptr<UpdateStatement> parseUpdateStatement();
        std::shared_ptr<DeleteStatement> parseDeleteStatement();
        std::shared_ptr<CreateTableStatement> parseCreateStatement();
        std::shared_ptr<DropTableStatement> parseDropStatement();

        // 子句解析
        std::vector<std::shared_ptr<Expression>> parseSelectList();
        std::shared_ptr<FromClause> parseFromClause();
        std::shared_ptr<ColumnDefinition> parseColumnDefinition();
        std::string parseDataType();

        // 表达式解析
        std::shared_ptr<Expression> parseExpression();
        std::shared_ptr<Expression> parseOrExpression();
        std::shared_ptr<Expression> parseAndExpression();
        std::shared_ptr<Expression> parseNotExpression();
        std::shared_ptr<Expression> parseComparisonExpression();
        std::shared_ptr<Expression> parseAdditiveExpression();
        std::shared_ptr<Expression> parseMultiplicativeExpression();
        std::shared_ptr<Expression> parsePrimaryExpression();
    };

} // namespace MiniDB
